<?php
class CommonAction extends BaseAction
{
    public function _initialize()
    {
        !(MODULE_NAME == 'Public') ? (!getLoginUid() ? $this->getToLogin() : NULL) : NULL;
        //获取登陆用户信息
        parent::_initialize();
        $this->resetWebTitle($this->getActionName());
    }

    public function getToLogin()
    {
        $this->redirect('/Admin/Public/login/');
    }

    /**
     * 获取列表
     */
    public function index()
    {
        $this->_getList();
    }

    public function add()
    {
        return $this->_add();
    }

    public function edit()
    {
        $this->_edit();
    }

    public function del()
    {
        $this->_del();
    }

    public function close()
    {
        $this->_updateStatus();
    }

    public function enable()
    {
        $this->_updateStatus(null, 1);
    }

    /**
     * 获取文章列表
     * @param null $Model
     * @param null $map
     */
    public function _getList($Model = null, $map = null, $return = false)
    {
        //page:1 rows:10 sort:itemid order:desc
        if (empty($Model)) {
            $Model = M($this->getActionName());
        }
        $sort = I('get.sort');
        $order = I('get.order');
        $sort = $sort ? $sort : $Model->getPk();
        $order = $order ? $order : 'desc';
        //取得满足条件的记录数
        $count = $Model->where($map)->count();
        if ($count > 0) {
            import("ORG.Util.Page");
            $Page = new Page($count, C('PAGE_SIZE'));
            $voList = $Model->where($map)->order("`" . $sort . "` " . $order)->limit($Page->firstRow . ',' . $Page->listRows)->select();
            $this->assign('page', $Page->show());
        }
        $this->assign('list', $voList);
        if ($return) {
            return $voList;
        } else {
            $this->display();
        }
    }

    /**
     * 添加
     * @param null $data
     * @param null $model
     */
    public function _add($data = null, $model = null, $isReturn = false)
    {
        if (IS_POST) {
            $name = !empty($model) ? $model : $this->getActionName();
            $Model = D($name);
            $data = $Model->create($data);
            if (empty($data)) {
                $this->error($Model->getError());
            }
            /*保存当前数据对象 */
            $rs = $Model->add($data);
            if ($isReturn) {
                return $rs;
            } else {
                if (!empty($rs)) { //保存成功
                    $this->success('添加成功！',U('/Admin/Course'));
                } else {
                    $this->error('添加失败！');
                }
            }
            exit;
        }
        $this->display();
    }

    protected function _edit($isReturn = false)
    {
        $name = $this->getActionName();
        $Model = M($name);
        $pk = $Model->getPk();
        $id = I($pk);
        $map = array();
        $map[$pk] = $id;
        $vo = $Model->where($map)->find();
        $this->checkEmpty($vo, '编辑信息不存在');
        if (IS_POST) {
            $this->_update(null, null, $isReturn);
        }
        if ($this->isAjax()) {
            if ($vo !== false) { // 读取成功
                $this->ajaxReturn($vo, "", 0);
            } else {
                die;
            }
        }
        if ($isReturn) {
            return $vo;
        } else {
            $this->assign('vo', $vo);
            $this->display();
        }
    }

    /**
     * 修改
     * @param null $data
     * @param null $model
     */
    public function _update($data = null, $model = null, $isReturn = false)
    {
        $name = !empty($model) ? $model : $this->getActionName();
        $Model = D($name);
        if (IS_POST) {
            $data = $Model->create($data);
            $pk = $Model->getPk();
            if (empty($data[$pk])) {
                $data[$pk] = I($pk);
            }
            if (empty($data)) {
                $this->error($Model->getError());
            }

            if(($data['title'] == null) || ($data['description'] == null)) {
                $this->error("数据填充完整 或者 title不能与其他课程重名");
            }
            $rs = $Model->save($data);
            if ($isReturn) {
                return $rs;
            } else {
                if (!empty($rs)) {
                    $this->success('编辑成功！',U('/Admin/Course'));
                } else {
                    $this->error('编辑失败');
                }
            }
            exit;
        }
    }

    /**
     * 删除操作
     * @param null $id
     * @param bool $return
     * @return mixed
     */
    protected function _del($id = null, $return = false)
    {
        $name = $this->getActionName();
        $Model = M($name);
        $pk = $Model->getPk();
        if (empty($id)) {
            $id = I($pk);
        }
        if (!empty($id)) {
            if (is_array($id)) {
                $where[$pk] = array("in", array_filter($id));
            } else {
                $where[$pk] = array('in', array_filter(explode(',', $id)));
            }
            $result = $Model->where($where)->delete();
            if ($return) {
                return $result;
            } else {
                if (!empty($result)) {
                    $this->success("成功删除{$result}条!");
                } else {
                    $this->error('没有删除任何数据!');
                }
            }
        }
    }

    public function _updateStatus($id = null, $status = 0, $return = false)
    {
        $name = $this->getActionName();
        $Model = M($name);
        $pk = $Model->getPk();
        if (empty($id)) {
            $id = I($pk);
        }
        if (!empty($id)) {
            if (is_array($id)) {
                $where[$pk] = array("in", array_filter($id));
            } else {
                $where[$pk] = array('in', array_filter(explode(',', $id)));
            }
            $data = array();
            $data['status'] = $status;
            $result = $Model->where($where)->save($data);
            if ($return) {
                return $result;
            } else {
                if (!empty($result)) {
                    $this->success("成功修改{$result}条!");
                } else {
                    $this->error('没有修改任何数据!');
                }
            }
        }
    }
}